import sys
import os
import random
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *

class Demo(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        layout = QVBoxLayout()

        toolBar = QToolBar()
        
        btn = QPushButton('Add')
        btn.clicked.connect(self.OnAddClicked)
        toolBar.addWidget(btn)

        btn = QPushButton('Insert')
        btn.clicked.connect(self.OnInsertClicked)
        toolBar.addWidget(btn)

        '''btn = QPushButton('Delete')
        btn.clicked.connect(self.OnDeleteClicked)
        toolLayout.addWidget(btn)'''
        layout.addWidget(toolBar)

        self.toolbox = QToolBox()
        self.toolbox.currentChanged.connect(self.OnCurrentChanged)

        buttonWidget = QWidget()
        self.buttonLayout = QVBoxLayout()
        self.buttonLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.buttonLayout.addWidget(QPushButton('PushButton',objectName='PushButton'))
        self.buttonLayout.addWidget(QRadioButton('RadioButton',objectName='RadioButton'))
        toolbtn=QToolButton(objectName='ToolButton')
        toolbtn.setIcon(QIcon(f'{self.parent.getAppDir()}/resources/confirm.png'))
        self.buttonLayout.addWidget(toolbtn)
        self.buttonLayout.addWidget(QCommandLinkButton('CommandLikButton',objectName='CommandLikButton'))
        buttonWidget.setLayout(self.buttonLayout)
        self.toolbox.addItem(buttonWidget,'Buttons')
        #self.toolbox.addItem(QPushButton('PushButton'),'Buttons')
        #self.toolbox.addItem(QRadioButton('RadioButton'),'Buttons')
        #self.toolbox.addItem(QToolButton(),'Buttons')
        #self.toolbox.addItem(QCommandLinkButton('CommandLikButton'),'Buttons')
        self.toolbox.addItem(QListView(),'Views')
        layout.addWidget(self.toolbox)

        self.setLayout(layout)
    def OnAddClicked(self):
        widgetName = f'Checkbox{random.randint(1,100)}'
        self.buttonLayout.addWidget(QCheckBox(widgetName,objectName=widgetName))
        self.parent.OnShowMessage(f"Add Checkbox:{widgetName}")
    def OnInsertClicked(self):
        index = self.toolbox.currentIndex()
        if index ==-1:
            index=0        
        widgetName = f'Checkbox{random.randint(1,100)}'
        self.buttonLayout.insertWidget(index,QCheckBox(widgetName,objectName=widgetName))
        self.parent.OnShowMessage(f"Insert {widgetName} at {index}")
    '''def OnDeleteClicked(self):
        current = self.toolbox.currentWidget()
        if current is None:return        
        self.parent.OnShowMessage(f"Delete item,Name={current.objectName()}")
        self.buttonLayout.removeWidget(current)'''
    def OnCurrentChanged(self,index):
        self.parent.OnShowMessage(f"Current index={index}")
def runDemo(parent):
    wigdet =  Demo(parent)
    return wigdet